//
//  SISSContractShortageOfLoanCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/10/9.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractShortageOfLoanCell.h"
#import "SISSTitleView.h"
#import "SISSTextView.h"

@interface SISSContractShortageOfLoanCell()<UITextFieldDelegate>

/// topView
@property (weak, nonatomic) SISSTitleView *topView;
/// checkOneButton
@property (weak, nonatomic) UIButton *checkOneButton;
/// checkOneButton
@property (weak, nonatomic) UIButton *checkTwoButton;


/// oneLabel
@property (weak, nonatomic) YYLabel *oneLabel;
/// twoLabel
@property (weak, nonatomic) YYLabel *twoLabel;

/// textField
@property (weak, nonatomic) UITextField *textField;

/* oneLine */
@property (weak, nonatomic) UIView *oneLine;


/// checkedButton
@property (weak, nonatomic) UIButton *checkedButton;

@end
@implementation SISSContractShortageOfLoanCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    self.checkedButton = contractModel.firstIndex == 1 ? self.checkOneButton : (contractModel.firstIndex == 2 ? self.checkTwoButton : nil);
    self.checkedButton.selected = self.checkedButton ? YES : NO;
    self.textField.userInteractionEnabled = contractModel.firstIndex == 2;
    self.textField.text = contractModel.rightStr;
}




#pragma mark - 点击事件
- (void)checkButtonClickAction:(UIButton *)button {
    self.checkedButton.selected = NO;
    self.checkedButton = button;
    self.checkedButton.selected = !button.selected;
    self.textField.userInteractionEnabled = button.tag == 101;
    self.contractModel.firstIndex = button.tag - 99;
    if (button.tag != 101)  self.textField.text = @"";
    if (button.tag == 100) {
        self.textField.text = @"";
        self.contractModel.isCheckedTwo = NO;
        self.contractModel.isChecked = button.isSelected;
        
    } else {
//        self.textField.text = @"";
        self.contractModel.isChecked = NO;
        self.contractModel.isCheckedTwo = button.isSelected;
        
    }
    
}


- (void)textFieldDidEndEditing:(UITextField *)textField {

    self.contractModel.rightStr = textField.text;
}





#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    SISSTitleView *topView = [[SISSTitleView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    topView.titleStr = @"发放贷款不足以支付房款余额时";
    topView.titleColor = MAIN_TEXT_COLOR;
    [self.contentView addSubview:topView];
    self.topView = topView;
    
    UIButton *button = [self creatButtonWithTag:100];
    [self.contentView addSubview:button];
    self.checkOneButton = button;
    
    YYLabel *oneLabel = [[YYLabel alloc] init];
    oneLabel.numberOfLines = 0;
    oneLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    oneLabel.textColor = SECONDARY_TEXT_COLOR;
    oneLabel.font = [UIFont systemFontOfSize:16];
    oneLabel.text = @"买方应于银行/住房公积金管理中心出具贷款承诺函之日起十日内补足应交款";
    [self.contentView addSubview:oneLabel];
    self.oneLabel = oneLabel;
    
    UIView *oneLine = [[UIView alloc] init];
    oneLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:oneLine];
    self.oneLine = oneLine;
    
    UIButton *checkTwoButton = [self creatButtonWithTag:101];
    [self.contentView addSubview:checkTwoButton];
    self.checkTwoButton = checkTwoButton;
    
    UITextField *textField = [[UITextField alloc] init];
    textField.userInteractionEnabled = NO;
    self.textField = textField;
    NSMutableAttributedString *twoAttributedText = [[NSMutableAttributedString alloc] initWithString:@"合同解除，卖方应在解除合同之日起三日内全额退还买方已付" attributes:nil];
    
    YYLabel *twoLabel = [[YYLabel alloc] init];
    twoLabel.textColor = SECONDARY_TEXT_COLOR;
    twoLabel.attributedText = [self getTextAttritedStrWithAttachText:twoAttributedText strs:@[@"等所有款项。"] textFields:@[textField] keyboardType:UIKeyboardTypeDecimalPad placeHolders:@[@"请填写已支款项"]];
    
    twoLabel.preferredMaxLayoutWidth = (SCREEN_WIDTH - 56);
    twoLabel.numberOfLines = 0;
    twoLabel.userInteractionEnabled = YES;
    [self.contentView addSubview:twoLabel];
    self.twoLabel = twoLabel;
    
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;
    
    
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(5 * margin);
    }];
    
    [self.oneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topView.mas_bottom).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.left.equalTo(weakSelf.contentView).offset(5 * margin);
    }];
    
    [self.checkOneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(1.5 * margin);
        make.centerY.equalTo(weakSelf.oneLabel);
    }];
    [self.oneLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLabel.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
    [self.twoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.oneLine.mas_bottom).offset(margin);
        make.left.right.equalTo(weakSelf.oneLabel);
    }];
    [self.checkTwoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkOneButton);
        make.centerY.equalTo(weakSelf.twoLabel);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.twoLabel.mas_bottom).offset(margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}


@end
